|
1
|
|
|
class Node { |
|
2
|
|
|
// put your code here to implement a link list node |
|
3
|
|
|
|
|
4
|
|
|
// in Javascript, null means this variable has not been set |
|
5
|
|
|
// null means this variable has been set to value null |
|
6
|
|
|
constructor(data, next = null) { |
|
7
|
|
|
this.data = data; |
|
8
|
|
|
this.next = next; |
|
9
|
|
|
} |
|
10
|
|
|
} |
|
11
|
|
|
|
|
12
|
|
|
class Linklist { |
|
13
|
|
|
// put your code here to implement link list |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* constructor |
|
17
|
|
|
* @param Node node optional |
|
18
|
|
|
*/ |
|
19
|
|
|
constructor() { |
|
20
|
|
|
this.head = null; |
|
21
|
|
|
this.size = 0; |
|
22
|
|
|
} |
|
23
|
|
|
/** |
|
24
|
|
|
* return the head element of link list |
|
25
|
|
|
* @return Node|null |
|
26
|
|
|
*/ |
|
27
|
|
|
getHead() { |
|
28
|
|
|
return this.head; |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* return the tail element of link list |
|
33
|
|
|
* @return Node|null |
|
34
|
|
|
*/ |
|
35
|
|
|
getTail() { |
|
36
|
|
|
if (!this.head) { |
|
37
|
|
|
return null; |
|
38
|
|
|
} |
|
39
|
|
|
let walker = this.head; |
|
40
|
|
|
while (walker.next) { |
|
41
|
|
|
walker = walker.next; |
|
42
|
|
|
} |
|
43
|
|
|
return walker; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* return the element at index of link list |
|
48
|
|
|
* @param integer index |
|
49
|
|
|
* @return Node|null |
|
50
|
|
|
*/ |
|
51
|
|
|
getElementAt(index) { |
|
52
|
|
|
if (!this.head || index >= this.size || index < 0) { |
|
53
|
|
|
return null; |
|
54
|
|
|
} |
|
55
|
|
|
let walker = this.head, |
|
56
|
|
|
i = 0; |
|
57
|
|
|
while (i < index) { |
|
58
|
|
|
walker = walker.next; |
|
59
|
|
|
i++; |
|
60
|
|
|
} |
|
61
|
|
|
return walker; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* return the size of link list |
|
66
|
|
|
* @return integer |
|
67
|
|
|
*/ |
|
68
|
|
|
getSize() { |
|
69
|
|
|
return this.size; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* return if link list is empty |
|
74
|
|
|
* @return boolean |
|
75
|
|
|
*/ |
|
76
|
|
|
isEmpty() { |
|
77
|
|
|
return (this.size === 0); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* insert a node to link list at head, return true if success |
|
82
|
|
|
* @param data |
|
83
|
|
|
* @return boolean |
|
84
|
|
|
*/ |
|
85
|
|
|
insertAtHead(data) { |
|
86
|
|
|
this.head = new Node(data, this.head); |
|
87
|
|
|
this.size++; |
|
88
|
|
|
return true; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* insert a node to link list at tail, return true if success |
|
93
|
|
|
* @param data |
|
94
|
|
|
* @return boolean |
|
95
|
|
|
*/ |
|
96
|
|
|
insertAtTail(data) { |
|
97
|
|
|
if (!this.head) { |
|
98
|
|
|
this.head = new Node(data); |
|
99
|
|
|
this.size++; |
|
100
|
|
|
return true; |
|
101
|
|
|
} |
|
102
|
|
|
let last = this.getTail(); |
|
103
|
|
|
last.next = new Node(data); |
|
104
|
|
|
this.size++; |
|
105
|
|
|
return true; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* insert a node to link list at head, return true if success |
|
110
|
|
|
* @param integer index |
|
111
|
|
|
* @param data |
|
112
|
|
|
* @return boolean |
|
113
|
|
|
*/ |
|
114
|
|
|
insertAt(index, data) { |
|
115
|
|
|
if (index > this.size || index < 0) { |
|
116
|
|
|
return false; |
|
117
|
|
|
} |
|
118
|
|
|
// insert at head |
|
119
|
|
|
if (!this.head) { |
|
120
|
|
|
return this.insertAtHead(data); |
|
121
|
|
|
} |
|
122
|
|
|
let previous = this.getElementAt(index - 1); |
|
123
|
|
|
previous.next = new Node(data, previous.next); |
|
124
|
|
|
this.size++; |
|
125
|
|
|
return true; |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
/** |
|
129
|
|
|
* remove head element of link list |
|
130
|
|
|
* @return data|null |
|
131
|
|
|
*/ |
|
132
|
|
|
removeAtHead() { |
|
133
|
|
|
if (this.size) { |
|
134
|
|
|
let result = this.head.data; |
|
135
|
|
|
this.head = this.head.next; |
|
136
|
|
|
this.size--; |
|
137
|
|
|
return result; |
|
138
|
|
|
} |
|
139
|
|
|
return null; |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
/** |
|
143
|
|
|
* remove tail element of link list |
|
144
|
|
|
* @return data|null |
|
145
|
|
|
*/ |
|
146
|
|
|
removeAtTail() { |
|
147
|
|
|
if (!this.head) { |
|
148
|
|
|
return null; |
|
149
|
|
|
} |
|
150
|
|
|
if (this.size === 1) { |
|
151
|
|
|
let result = this.head.data; |
|
152
|
|
|
this.head = null; |
|
153
|
|
|
this.size--; |
|
154
|
|
|
return result; |
|
155
|
|
|
} |
|
156
|
|
|
let walker = this.head; |
|
157
|
|
|
while (walker.next.next) { |
|
158
|
|
|
walker = walker.next; |
|
159
|
|
|
} |
|
160
|
|
|
let result = walker.next.data; |
|
161
|
|
|
walker.next = null; |
|
162
|
|
|
this.size--; |
|
163
|
|
|
return result; |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
/** |
|
167
|
|
|
* remove element at index of link list |
|
168
|
|
|
* @param integer index |
|
169
|
|
|
* @return data |
|
170
|
|
|
*/ |
|
171
|
|
|
removeAt(index) { |
|
172
|
|
|
if (index >= this.size) { |
|
173
|
|
|
return null; |
|
174
|
|
|
} |
|
175
|
|
|
// remove at head |
|
176
|
|
|
if (index === 0) { |
|
177
|
|
|
return this.removeAtHead(); |
|
178
|
|
|
} |
|
179
|
|
|
if (index === (this.size - 1)) { |
|
180
|
|
|
return this.removeAtTail(); |
|
181
|
|
|
} |
|
182
|
|
|
let previous = this.getElementAt(index - 1), |
|
183
|
|
|
result = previous.next.data; |
|
184
|
|
|
previous.next = previous.next.next; |
|
185
|
|
|
this.size--; |
|
186
|
|
|
return result; |
|
187
|
|
|
} |
|
188
|
|
|
|
|
189
|
|
|
/** |
|
190
|
|
|
* visit all elements of link list from head to tail |
|
191
|
|
|
* @return array |
|
192
|
|
|
*/ |
|
193
|
|
|
traverse() { |
|
194
|
|
|
if (this.size) { |
|
195
|
|
|
let walker = this.head, |
|
196
|
|
|
result = []; |
|
197
|
|
|
while (walker.next) { |
|
198
|
|
|
result.push(walker.data); |
|
199
|
|
|
walker = walker.next; |
|
200
|
|
|
} |
|
201
|
|
|
result.push(walker.data); |
|
202
|
|
|
return result; |
|
203
|
|
|
} |
|
204
|
|
|
return []; |
|
205
|
|
|
} |
|
206
|
|
|
|
|
207
|
|
|
/** |
|
208
|
|
|
* clear link list |
|
209
|
|
|
*/ |
|
210
|
|
|
clear() { |
|
211
|
|
|
this.head = null; |
|
212
|
|
|
this.size = 0; |
|
213
|
|
|
} |
|
214
|
|
|
} |
|
215
|
|
|
|
|
216
|
|
|
module.exports = { |
|
217
|
|
|
Linklist, |
|
218
|
|
|
Node |
|
219
|
|
|
}; |